<?php
/**
 * FunAdmin
 * ============================================================================
 * 版权所有 2017-2028 FunAdmin，并保留所有权利。
 * 网站地址: http://www.FunAdmin.com
 * ----------------------------------------------------------------------------
 * 采用最新Thinkphp8实现
 * ============================================================================
 * Author: yuege
 * Date: 2017/8/2
 */
namespace app\admin\controller;
use app\admin\service\AuthService;
use app\BaseController;
use Exception;
use think\App;

class Login extends BaseController {
    protected $layout='';
    protected $middleware = [
//        ViewNode::class,
    ];
    public function __construct(App $app) {

        parent::__construct($app);
    }

    //登录
    public function index() {
        if (!$this->request->isPost()) {
            return view('auth/passport/login');
        } else {
            $post  = $this->request->post() ;
            $username = $this->request->post('username', '', ['strip_tags','trim','htmlspecialchars']);
            $password = $this->request->post('password', '',['strip_tags','trim','htmlspecialchars']);
            $rule = [
                "username|用户名" => 'require',
                "password|密码" => 'require',
            ];
            $this->validate($post, $rule);
            // 用户信息验证
            try {
                $auth = AuthService::instance();
                $auth->checkLogin($username, $password, false);
            } catch (Exception $e) {
                $this->error(lang('Login Failed')."：{$e->getMessage()}");
            }
            $this->redirect(__u('admin/index/index'));
        }
    }

}
